﻿using GameFramework.Battle.Core;
using System;
using System.Collections.Generic;
using UnityEngine;
using WorldMapBase;
using Yoozoo.HRP.Runtime;

namespace Yoozoo.Gameplay.RTS
{
#pragma warning disable 0429
    /// <summary>
    /// 没有动画的MeshInstancing
    /// </summary>
    public class ModelGpuInstancing : MonoBehaviour
    {
        public int poolKey;
        public Material mat;
        public Mesh shadowMesh;
        public Material shadowMat;
        
        public Mesh mesh;

        private GPUInstance_SpawnObject m_spawnObject;

        public Transform gunPoint;

        public int mountPointIndex;
        
        public bool m_displaySubMesh1;
        public bool m_riming;
        public float m_rimInstencity;
        public Color m_rimColor;
        private float m_rimTime = 0.1f;
        private float m_rimSpeed = 10;
        private float m_rimCurTime;
        private float m_sinTime;
        
        private int m_instancingId;

        private void OnDestroy()
        {
            RemoveSpawnObject();
        }
        
        protected Vector3 position;
        public float localScale = 0.1f;
        private string cullingCameraMark;
        public int CullingLayer => cullingLayer;
        private int cullingLayer;
        
        public int SetConfig()
        {
            //shadowMat.SetFloat("_ShadowHeight", GlobalValue.ShadownHeight);
            int instancingId = GPUInstancingManager.AddGpuInstancingConfig(poolKey, mat, mesh,  shadowMat);
            m_spawnObject = GPUInstancingManager.AddGpuInstanceSpawnObject(instancingId, transform);

            m_rimColor = Color.black;
            m_riming = false;
            m_rimInstencity = 0;
            m_spawnObject.instancingId = instancingId;
            m_instancingId = instancingId;
            return instancingId;
        }

        public void SetParent(Transform transform)
        {
            m_spawnObject.SetParent(transform);
            //m_spawnObject.SetTransformOffset(this.transform);
            if (gunPoint != null)
            {
                var localPos = gunPoint.localPosition;
                var localRot = gunPoint.localRotation;
                var localScale = gunPoint.localScale;
                gunPoint.SetParent(transform);
                gunPoint.localPosition = localPos;
                gunPoint.localRotation = localRot;
                gunPoint.localScale = localScale;
            }
        }
        
        public void OnRelease()
        {
           RemoveSpawnObject();
        }

        private void RemoveSpawnObject()
        {
            if (m_spawnObject != null)
            {
                GPUInstancingManager.RemoveGpuSpawnObject(m_spawnObject, poolKey);
                m_spawnObject = null;
            }
            ResetData();
        }

        private void ResetData()
        {
            //m_instancingId = 0;
        }

        public void SetRimColor(Color color)
        {
            m_rimColor = color;
        }
        
        public void SetRimValue(float rimValue)
        {
            /*if (m_spawnObject != null)
            {
                m_gpuAnimator.SetRim(m_instancingId, m_rimColor, 1, rimValue);
            }*/
        }

        public void SetShadowHeight(float shadowHeight)
        {
            m_spawnObject?.SetShadowHeight(shadowHeight);
        }

        public void SetShadowAlpha(float alpha)
        {
            m_spawnObject?.SetShadowAlpha(alpha);
        }
    }
}
